//
//  RBGroupAloneBuyVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/7/27.
//

#import "RBGroupAloneBuyVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBPager.h"
#import "RanTool.h"
#import "UIViewController+RBAdd.h"
#import "RBGroupPayVC.h"
#import "RBGroupStartResponse.h"
#import "UIImageView+RBAdd.h"
#import "RBAddresssListVC.h"
#import "RBAddressDetailResponse.h"
#import "RBGroupGenerateOrderResponse.h"

@interface RBGroupAloneBuyVC ()

@property(nonatomic, strong)UIScrollView *scrollView;

/** 0 快递 1自取 */
@property(nonatomic, assign) NSInteger addressTypeIndex;
@property(nonatomic, strong)IBOutlet UIView *addressView;
@property(nonatomic, strong)IBOutlet UILabel *addressNameL;
@property(nonatomic, strong)IBOutlet UILabel *addressL;
@property(nonatomic, strong)IBOutlet UIView *ziti_addressView;
@property(nonatomic, strong)IBOutlet UILabel *ziti_addressL;

@property(nonatomic, strong)IBOutlet UIView *shopView;
@property(nonatomic, strong)IBOutlet UIImageView *shopImgView;
@property(nonatomic, strong)IBOutlet UILabel *shopNameL;
@property(nonatomic, strong)IBOutlet UIImageView *goodsImgView;
@property(nonatomic, strong)IBOutlet UILabel *goodsNameL;
@property(nonatomic, strong)IBOutlet UILabel *goodsAttL;
@property(nonatomic, strong)IBOutlet UILabel *goodsNumL;
@property(nonatomic, strong)IBOutlet UILabel *goodsPriceL;

@property(nonatomic, strong)IBOutlet UIView *noteView;
@property(nonatomic, strong)IBOutlet UITextView *noteTextView;

@property(nonatomic, strong)IBOutlet UIView *bottomView;
@property(nonatomic, strong)IBOutlet UILabel *totalPriceL;

@property(nonatomic, strong) RBGroupStartResponse *response;
@property(nonatomic, strong) RBAddressListModel *addressModel;
@property(nonatomic, strong) RBGroupStartZitiModel *zitiModel;

@end

@implementation RBGroupAloneBuyVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self getData];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self getAddressDetail];
}

#pragma mark - Private

- (void)setup
{
    self.title = @"确认购买";
    [self.bottomView zoomSubViewsWithSelf];
    [self.view addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(Zoom(self.bottomView.height));
        make.bottom.mas_equalTo(-kRBSafeBottomHeight);
    }];
    self.bottomView.hidden = YES;
    self.addressNameL.numberOfLines = 0;
    self.addressL.numberOfLines = 0;
    [self.addressView rbAddTapGesWithTarget:self action:@selector(addressViewTapped)];
    [self.ziti_addressView rbAddTapGesWithTarget:self action:@selector(zitiAddressViewTapped)];
    [self.shopView zoomSubViewsWithSelf];
    self.shopView.height = Zoom(self.shopView.height);
    [self.noteView zoomSubViewsWithSelf];
    self.noteView.height = Zoom(self.noteView.height);
}

- (void)reloadAddressDisplay
{
    if(self.addressModel)
    {
        self.addressNameL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
            [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(15)] color:[RBThemeConfig textBlackColor] string:[NSString stringWithFormat:@"收货人：%@", self.addressModel.name] lineSpacing:Zoom(4)];
            [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(15)] color:[RBThemeConfig textBlackColor] string:@"　" lineSpacing:Zoom(4)];
            [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(13)] color:[UIColor colorWithHexString:@"#969593"] string:self.addressModel.mobile lineSpacing:Zoom(4)];
        }];
        self.addressL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
            [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(14)] color:[UIColor colorWithHexString:@"#4D4948"] string:[NSString stringWithFormat:@"%@ %@ %@ %@", self.addressModel.province, self.addressModel.city, self.addressModel.area, self.addressModel.address] lineSpacing:Zoom(4)];
        }];
    }
    else
    {
        self.addressNameL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
            [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(15)] color:[RBThemeConfig textBlackColor] string:@"收货地址" lineSpacing:Zoom(4)];
            }];
        self.addressL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
            [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(14)] color:[UIColor colorWithHexString:@"#4D4948"] string:@"请选择" lineSpacing:Zoom(4)];
        }];
    }
    if(self.zitiModel)
    {
        NSMutableString *stringM = NSMutableString.string;
        [stringM appendFormat:@"门店:%@",self.zitiModel.title];
        [stringM appendFormat:@"\n地址:%@ %@ %@ %@",kRBNotNilString(self.zitiModel.add_pro),kRBNotNilString(self.zitiModel.add_city),kRBNotNilString(self.zitiModel.add_area), kRBNotNilString(self.zitiModel.address)];
        [stringM appendFormat:@"\n营业时间:%@ %@",kRBNotNilString(self.zitiModel.yingye_week),kRBNotNilString(self.zitiModel.yingye_time)];
        self.ziti_addressL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
            [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(13)] color:[UIColor colorWithHexString:@"#4D4948"] string:stringM lineSpacing:Zoom(7)];
        }];;
    }
    else
    {
        self.ziti_addressL.text = @"请选择自提点";
    }
}

- (void)reloadPriceDisplay
{
    self.totalPriceL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
        [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(16)] color:[UIColor colorWithHexString:@"#F23030"] string:@"￥"];
        [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(22)] color:[UIColor colorWithHexString:@"#F23030"] string:self.type == 0? self.response.data.goods.goods_price :self.response.data.goods.group_price];
    }];
}

- (void)fillData
{
    [self.scrollView rbSetContentWidth:kRBMainScreenWidth];
    
    [self reloadAddressDisplay];
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(10)];
        [view view_addView:({
            UIView *view = UIView.new;
            [view h_view_addView:[self addressTypeViewWithTitle:@"快递邮寄" tag:0] withHeight:CGFLOAT_MIN width:Zoom(175) top:0 bottom:0 left:0 right:CGFLOAT_MIN centerY:NO];
            [view h_view_addView:[self addressTypeViewWithTitle:@"门店自提" tag:1] withHeight:CGFLOAT_MIN width:Zoom(175) top:0 bottom:0 left:0 right:CGFLOAT_MIN centerY:NO];
            view;
        }) withHeight:Zoom(50) width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        [view view_addView:({
            UIView *view = UIView.new;
            view.backgroundColor = [RBThemeConfig lineGrayColor];
            view;
        }) withHeight:1 width:Zoom(326) top:0 bottom:0 left:0 right:0 centerX:1];
        if(self.addressTypeIndex == 0)
        {
            [view view_addView:self.addressView withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:0 left:0 right:0 centerX:0];
        }
        else
        {
            [view view_addView:self.ziti_addressView withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        }
        view;
    }) withHeight:CGFLOAT_MIN width:Zoom(350) top:Zoom(10) bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
    
    self.shopNameL.text = self.response.data.store_name;
    [self.goodsImgView setImageWithUrl:self.response.data.goods.picurl placeHolder:nil];
    self.goodsNameL.text = self.response.data.goods.good_name;
    self.goodsAttL.text = @"　";
    self.goodsPriceL.text = [NSString stringWithFormat:@"¥%@", self.type == 0? self.response.data.goods.goods_price :self.response.data.goods.group_price];
    [self.scrollView addView:self.shopView withHeight:self.shopView.height width:Zoom(350) top:Zoom(10) bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
    
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(10)];
        
        [view view_addView:UIView.new withHeight:Zoom(15) width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        
        [view view_addView:[self itemViewWithTitle:@"商品小计" des:[NSString stringWithFormat:@"¥%@", self.type == 0? self.response.data.goods.goods_price :self.response.data.goods.group_price]] withHeight:Zoom(30) width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(15) right:Zoom(12) centerX:0];
        if(self.addressTypeIndex == 0)
        {
            [view view_addView:[self itemViewWithTitle:@"运费" des:@"¥0.00"] withHeight:Zoom(30) width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(15) right:Zoom(12) centerX:0];
        }
        
        [view view_addView:UIView.new withHeight:Zoom(15) width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        
        view;
    }) withHeight:CGFLOAT_MIN width:Zoom(350) top:Zoom(10) bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
    
    [self.scrollView addView:self.noteView withHeight:self.noteView.height width:Zoom(350) top:Zoom(10) bottom:Zoom(10) left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
    
    self.bottomView.hidden = NO;
    [self reloadPriceDisplay];
}

#pragma mark - Network

- (void)getData
{
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"group_id"] = self.group_id? self.group_id: @"2";
    } path:@"hu_group_buy" post:YES log:YES responseClass:RBGroupStartResponse.class desView:self.view showHud:YES showPlaceHolder:YES sucBlock:^(RBGroupStartResponse *data) {
        weak_self.response = data;
        weak_self.addressModel = data.data.user_address;
        [weak_self fillData];
    } failBlock:nil reloadBlock:^{
        [weak_self getData];
    }];
}

- (void)getAddressDetail
{
    if(self.addressModel.add_id.length)
    {
        __weak typeof(self) weak_self = self;
        [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
            dicM[@"add_id"] = self.addressModel.add_id;
        } path:@"hu_address_detail" post:YES log:YES responseClass:RBAddressDetailResponse.class desView:self.view showHud:NO showPlaceHolder:NO sucBlock:^(RBAddressDetailResponse *data) {
            weak_self.addressModel = data.data;
            [weak_self reloadAddressDisplay];
        } failBlock:^(id data) {
            weak_self.addressModel = nil;
            [weak_self reloadAddressDisplay];
        } reloadBlock:nil];
    }
}

#pragma mark - Event

- (void)addressTypeItemViewTapped: (UITapGestureRecognizer *)ges
{
    if(self.addressTypeIndex == ges.view.tag) return;
    self.addressTypeIndex = ges.view.tag;
    [self fillData];
}

- (void)addressViewTapped
{
    __weak typeof(self) weak_self = self;
    RBAddresssListVC *vc = RBAddresssListVC.new;
    vc.selectBlock = ^(RBAddressListModel *model) {
        weak_self.addressModel = model;
        [weak_self reloadAddressDisplay];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)zitiAddressViewTapped
{
    __weak typeof(self) weak_self = self;
    UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"请选择自提点" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [vc addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    for (NSInteger i = 0; i < self.response.data.ziti_list.count; i++)
    {
        RBGroupStartZitiModel *model = self.response.data.ziti_list[i];
        [vc addAction:[UIAlertAction actionWithTitle:model.title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            weak_self.zitiModel = model;
            [weak_self reloadAddressDisplay];
        }]];
    }
    [self presentViewController:vc animated:YES completion:nil];

}

- (IBAction)confirmBtnTapped:(id)sender
{
    if(self.addressTypeIndex == 0 && !self.addressModel)
    {
        [self.view showTip:@"请选择收件地址" completion:nil];
        return;
    }
    if(self.addressTypeIndex == 1 && !self.zitiModel)
    {
        [self.view showTip:@"请选择自提点" completion:nil];
        return;
    }
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"group_activity_id"] = self.group_activity_id;
        dicM[@"group_id"] = self.group_id;
        dicM[@"action"] = @(self.addressTypeIndex + 1);
        dicM[@"add_id"] = self.addressModel.add_id;
        dicM[@"zt_id"] = self.zitiModel.zt_id;
    } path:({
        NSString *path = @"";
        if(self.type == 0)
        {
            path = @"hu_group_separate_buy_save";
        }
        else if(self.type == 1)
        {
            path = @"hu_group_buy_save";
        }
        else if(self.type == 2)
        {
            path = @"hu_group_ongoing_save";
        }
        path;
    }) post:YES log:YES responseClass:RBGroupGenerateOrderResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(RBGroupGenerateOrderResponse *response) {
        RBGroupPayVC *vc = RBGroupPayVC.new;
        vc.type = weak_self.type;
        vc.orderNum = response.data.order_number;
        vc.price = response.data.sale_price;
        vc.group_id = weak_self.group_id;
        vc.group_activity_id = response.data.group_activity_id.length? response.data.group_activity_id: weak_self.group_activity_id;
        [weak_self.navigationController pushViewController:vc animated:YES];
    } failBlock:^(RBGroupGenerateOrderResponse *response) {
        [weak_self.view showTip:response.message completion:nil];
    } reloadBlock:nil];
}

#pragma mark - Setter

#pragma mark - Getter

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = UIScrollView.new;
            view.backgroundColor = [RBThemeConfig bgGrayColor];
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.mas_equalTo(0);
                make.bottom.mas_equalTo(self.bottomView.mas_top);
            }];
            view;
        });
    }
    return _scrollView;
}

- (UIView *)addressTypeViewWithTitle: (NSString *)title tag: (NSInteger)tag
{
    UIView *view = UIView.new;
    view.tag = tag;
    [view rbAddTapGesWithTarget:self action:@selector(addressTypeItemViewTapped:)];
    [view view_addView:({
        UIView *view = UIView.new;
        [view h_view_addView:[RanTool imgViewWithMode:1 radius:0 imgName:self.addressTypeIndex == tag? @"addressListDefault": @"unsel"] withHeight:Zoom(15) width:Zoom(15) top:0 bottom:0 left:0 right:0 centerY:1];
        [view h_view_addView:[RanTool labelWithFontSize:Zoom(14) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:title] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(10) right:0 centerY:0];
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:1];
    return view;
}

- (UIView *)itemViewWithTitle: (NSString *)title des: (NSString *)des
{
    UIView *view = UIView.new;
    
    UIImageView *imgView = UIImageView.new;
    imgView.image = [UIImage imageNamed:@"mine_arrow_right"];
    [view addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.centerY.mas_equalTo(0);
        make.width.mas_equalTo(Zoom(7));
        make.height.mas_equalTo(Zoom(12));
    }];
    
    UILabel *titleL = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:title];
    [view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.centerY.mas_equalTo(0);
    }];
    
    UILabel *desL = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:2 text:des];
    [view addSubview:desL];
    [desL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(imgView.mas_left).offset(-Zoom(12));
    }];
    
    return view;
}

@end
